package cn.lzj.basic.service.impl;

import cn.lzj.basic.constants.VerifyCodeConstants;
import cn.lzj.basic.dto.SmsCodeDto;
import cn.lzj.basic.exception.BusinessException;
import cn.lzj.basic.service.IVerifyCodeService;
import cn.lzj.basic.util.StrUtils;
import cn.lzj.basic.util.VerifyCodeUtils;
import cn.lzj.user.domain.User;
import cn.lzj.user.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.concurrent.TimeUnit;

@Service
public class VerifyCodeServiceImpl implements IVerifyCodeService {
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private UserMapper userMapper;

    @Override
    public String imageCode(String key) {
        //1.判断key是否为空
        if (key.isEmpty()) {
            throw new BusinessException("key值为空");
        }
        //2.生成随机字符串验证码
        String code = StrUtils.getRandomString(4);
        //3.key为key ，value 为生成的验证码存到redis中
        redisTemplate.opsForValue().set(key, code, 4, TimeUnit.MINUTES);
        //4.生成验证码图片
        //5.验证码图片转换为string
        //6.返回数据
        return VerifyCodeUtils.verifyCode(115, 42, code);
    }

    /**
     * 短信验证码的实现
     *
     * @param smsCodeDto
     */
    @Override
    public void smsCode(SmsCodeDto smsCodeDto) {
        //1.空值校验
        String imageCode = smsCodeDto.getImageCode();//获取图形验证码
        String phoneCodeKey = smsCodeDto.getImageCodeKey();//获取图形验证码的key
        String phone = smsCodeDto.getPhone();//获取输入的手机号
        if (StringUtils.isEmpty(phone)) {
            throw new BusinessException("手机号不能为空!!!");
        }
        if (StringUtils.isEmpty(phoneCodeKey)) {
            throw new BusinessException("请刷新重新获取图片验证码");
        }
        if (StringUtils.isEmpty(imageCode)) {
            throw new BusinessException("图片验证码不能为空!!!");
        }
        Object redisCodekey = redisTemplate.opsForValue().get(phoneCodeKey);
        if (redisCodekey == null) {
            throw new BusinessException("图片验证码过期，请重新输入");
        }
        //2.校验图片验证码是否正确
        if (!redisCodekey.toString().equalsIgnoreCase(imageCode)) {
            throw new BusinessException("错误的图片验证码，请重新输入");
        }
        //3.校验,手机号码是否被注册
        User user = userMapper.loadByphone(phone);
        if (user != null) {
            throw new BusinessException("该手机已经注册，请直接登录!");
        }
        Object codeObj = redisTemplate.opsForValue().get(VerifyCodeConstants.REGISTER_CODE_PREFIX + phone);
        String code = null;
        if (codeObj != null) {
            String codeStr = (String) codeObj;
            String time = codeStr.split(":")[1];
            long intervalTime = System.currentTimeMillis() - Long.valueOf(time);
            if (intervalTime <= 1 * 60 * 1000) { //没过重发时间
                throw new BusinessException("请勿重复发送!");
            } else {
                code = codeStr.split(":")[0];
            }
        } else {//没有验证码
            code = StrUtils.getComplexRandomString(6);
        }
        //将验证码数据添加到redis
        redisTemplate.opsForValue().set(VerifyCodeConstants.REGISTER_CODE_PREFIX + phone,
                code + ":" + System.currentTimeMillis(), 3, TimeUnit.MINUTES);//存3分钟
        //发短信
        //SmsUtil.sendSms(phone, "你的验证码为：" + code + "，请在3分钟之内使用!" );  //有次数
        System.out.println("你的验证码为：" + code + "，请在3分钟之内使用!");//测试

    }

    /**
     * 短信验证码的实现
     *
     * @param smsCodeDto
     */
    @Override
    public void bindersmsCode(SmsCodeDto smsCodeDto) {
        //1.空值校验
        String phone = smsCodeDto.getPhone();//获取输入的手机号
        if (StringUtils.isEmpty(phone)) {
            throw new BusinessException("手机号不能为空!!!");
        }

        Object codeObj = redisTemplate.opsForValue().get(VerifyCodeConstants.BINDER_PREFIX + phone);
        String code = null;
        if (codeObj != null) {
            String codeStr = (String) codeObj;
            String time = codeStr.split(":")[1];
            long intervalTime = System.currentTimeMillis() - Long.valueOf(time);
            if (intervalTime <= 1 * 60 * 1000) { //没过重发时间
                throw new BusinessException("请勿重复发送!");
            } else {
                code = codeStr.split(":")[0];
            }
        } else {//没有验证码
            code = StrUtils.getComplexRandomString(6);
        }
        //将验证码数据添加到redis
        redisTemplate.opsForValue().set(VerifyCodeConstants.BINDER_PREFIX + phone,
                code + ":" + System.currentTimeMillis(), 3, TimeUnit.MINUTES);//存3分钟
        //发短信
        //SmsUtil.sendSms(phone, "你的验证码为：" + code + "，请在3分钟之内使用!" );  //有次数
        System.out.println("你的验证码为：" + code + "，请在3分钟之内使用!");//测试
    }

}
